<?php
/* 
* @Title:  [类型属性控制器]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-04 19:42:11
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-12 20:11:56
* @Copyright:  [hn7m.com]
*/
class AttrControl extends CommonControl{
	public $model=NULL;
	public $typeModel=NULL;

	/**
	 * [__init 自动加载模型类]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K('Attr');
		$this->typeModel=K('Type');
	}
	/**
	 * [Index 默认显示属性列表]
	 */
	public function Index(){
		$attr=$this->model->get_data();
		if(!empty($attr)){
			foreach ($attr as $key => $value) {
				$attr[$key]['qm_type_ttitle']=K('Type')->get_onedate_value($value['qm_type_tid'],'ttitle');
			}
		}
		$this->assign('attr',$attr);  //分配属性数据
		$tid=Q('get.tid');
		$this->assign('tid',$tid);
		$this->display();
	}

	/**
	 * [edit_attr AJAX修改属性]
	 * @return [type] [description]
	 */
	public function ajax_edit_attr(){
		$aid=Q('post.aid',0,'intval');
		$acontent=Q('post.acontent','','htmlspecialchars');
		$data=array('acontent'=>$acontent);
		if(IS_AJAX){
			if(!$this->model->create()){
				$return=array(
					'state'		=>0,
					'message'	=>$this->error($this->model->error),
					'timeout'	=>3
					);
				$this->ajax($return);
			}
			if($this->model->update_data($aid,$data)){
				$return=array(
					'state'		=>1,
					'message'	=>'恭喜您,更新成功!',
					'timeout'	=>3
					);
				$this->ajax($return);
			}
		}
		$this->display();
	}
	/**
	 * [add_attr 添加属性]
	 */
	public function add_attr(){
		$tid=Q('get.tid');
		$this->assign('tid',$tid);
		$ttitle=K('Type')->get_onedate_value($tid,'ttitle');
		$this->assign('ttitle',$ttitle);
		if(IS_POST){
			if(!$this->model->create()){
				$this->error($this->model->error);
			}
			if($this->model->add_attr_data($tid)){
				$this->success('恭喜您,添加成功!',U('index',array('tid'=>$tid)));
			}else{
				$this->error($this->model->error);
			}
		}
		$this->display();
	}
	/**
	 * [ajax_del_attr ajax删除属性]
	 * @return [type] [description]
	 */
	public function ajax_del_attr(){
		if(IS_AJAX){
			$aid=Q('aid',0,'intval');
			if($this->model->del_data($aid)){
				$return=array(
					'state'		=>1,
					'message'	=>'删除成功!',
					'timeout'	=>3
					);
				$this->ajax($return);
			}
		}
	}
}
?>